//
//  FirmwareController.swift
//  DaRings
//
//  Created by 易先生 on 2022/10/28.
//

import UIKit
import MYModule

open class FirmwareController: BaseViewController {
    
    
    public var brage: Bool = false

    open override func viewDidLoad() {
        super.viewDidLoad()
//        navigation.bar.backgroundColor =  UIColor.darkBackground
    }
    
    open override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.interactivePopGestureRecognizer?.delegate = self
    }
    
    open override func makeUI() {
        super.makeUI()
        
        self.navigation.bar.backgroundColor = UIColor.darkBackground
        view.backgroundColor =  UIColor.darkBackground
        
        view.addSubview(titleView)
        view.addSubview(tableView)
        
        titleView.snp.makeConstraints { make in
            make.top.equalTo(topLayoutGuide.snp.bottom)
            make.leading.equalToSuperview()
            make.trailing.equalToSuperview()
        }

        tableView.snp.makeConstraints { make in
            make.top.equalTo(titleView.snp.bottom)
            make.leading.trailing.bottom.equalToSuperview()
        }
    }
    
    open override func bindViewModel() {
        super.bindViewModel()
        viewModel.items.bind(to: tableView.rx.items(cellIdentifier: FirmwareHomeCell.reuseIdentifier, cellType: FirmwareHomeCell.self)) {[weak self] row, item, cell in
            
            cell.bind(to: item)
            
            cell.brage.isHidden = true
            
            if item == .information {
                
                cell.setRoundedCorners(.top, radius: 18)
            }else if item == .upgrade {
                
                cell.setRoundedCorners(.bottom, radius: 18)
                if let brage = self?.brage {
                    cell.brage.isHidden = !brage
                }
                
            }else{
                
                cell.setRoundedCorners(.all, radius: 0)
            }
        }.disposed(by: rx.disposeBag)
        
        tableView.rx.itemSelected.subscribe(onNext: { [weak self] IndexPath in
            guard let `self` = self else {return}
            let item = self.viewModel.items.value[IndexPath.row]
            self.gotoViewController(with: item)
        }).disposed(by: rx.disposeBag)
    }
    
    private func gotoViewController(with action: FirmwareAction) {
        switch action {
        case .information:
            let vc = FirmwareInformationController()
            self.navigationController?.pushViewController(vc, animated: true)
        case .reset:
            
            if BluetoothManager.shared.bluetoothState != .poweredOn {
                UIAlertController.showOkAlert(title: "dialog_firmware_reset_ble_powered_off_patiently".language, message: "", in: self) {
                    return
                }
                return
            }
            
            if BluetoothManager.shared.deviceState != .connected {
                UIAlertController.showOkAlert(title: "dialog_firmware_reset_ble_powered_off_patiently".language, message: "", in: self) {
                    return
                }
                return
            }
            
            UIAlertController.showConfirm(title: "other_settings_factory_reset_dialog_title".language, message: "other_settings_factory_reset_hint".language, in: self) { [weak self]_ in
                guard let `self` = self else {return}
                let vc = FirmwareResetController()
                self.navigationController?.pushViewController(vc, animated: true)
            }
        case .upgrade:
            
            upgradeAction()
            
            
        }
    }
    
    open func upgradeAction() {
        
        let vc = FirmwareUpgradeController()
        self.navigationController?.pushViewController(vc, animated: true)
        
    }
    
    private lazy var viewModel: FirmwareViewModel = {
        let viewModel = FirmwareViewModel()
        return viewModel
    }()
    
    private lazy var titleView: TopTitleView = {
        let titleView = TopTitleView()
        titleView.configTitle(title: "firmware_upgrade_title".language)
        return titleView
    }()
    
    private lazy var tableView: TableView = {
        let tableView = TableView(frame: .zero, style: .plain)
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.rowHeight = 54
        tableView.register(FirmwareHomeCell.self, forCellReuseIdentifier: FirmwareHomeCell.reuseIdentifier)
        return tableView
    }()
    
}
